<template>
    <el-drawer 
        v-model="showDrawer" 
        :title="title" 
        :size="size" 
        :close-on-click-modal="false" 
        :destory-on-close="destoryOnClose"
    >
        <div class="formDrawer">
            <div class="body">
                <slot></slot>
            </div>
            <div class="actions">
                <el-button type="primary" @click="submit" :loading="loading">{{ confirmText }}</el-button>
                <el-button type="default" @click="close">取消</el-button>
            </div>
        </div>
    </el-drawer>
</template>

<script setup>
import { ref } from 'vue'

// 设置初始抽屉状态
const showDrawer = ref(false)

// 初始化按钮loading状态
const loading = ref(false)

// 获取父组件传过来的参数
const props = defineProps({
    title: String,
    size: {
        type: String,
        default: '45%'
    },
    destoryOnClose: {
        type: Boolean,
        default: false
    },
    confirmText: {
        type: String,
        default: '确定'
    }
})

// 定义打开方法
const open = () => {
    showDrawer.value = true
}

// 定义关闭方法
const close = () => {
    showDrawer.value = false
}

// 定义按钮缓存方法-打开-设置loading状态为true
const openLoading = () => {
    loading.value = true

}

// 定义按钮缓存方法-关闭-设置loading状态为false
const closeLoading = () => {
    loading.value = false
}

// 暴露方法给父组件
defineExpose({
    open,
    close,
    openLoading,
    closeLoading
})

// 提交方法-广播方法
const emit = defineEmits(['submit'])

// 广播方法给父组件
const submit = () => emit('submit')

</script>

<style scoped>
.formDrawer{
    height: 100%;
    width: 100%;
    position: relative;
    @apply flex flex-col;
}
.formDrawer .body{
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 50px;
    overflow-y: auto;
}
.formDrawer .actions{
    height: 50px;
    @apply mt-auto flex items-center;
}
</style>
